/************************************************************************
 * The code is owned by mainframer.cn and Shang Tao
 * Without aproval of mainframer.cn and Shang Tao, removing the copyright
 * infomation is regarded as invassion of proprietary copyright
 ***********************************************************************/
package com.mr.qa.action;

import java.sql.Blob;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Transaction;

import com.mr.qa.bo.UploadedFile;
import com.mr.qa.dao.impl.UploadedFileDAO;
import com.mr.qa.util.HibernateSessionFactory;

public class UploadedFileAction extends BaseAction
{
    private static final Log log = LogFactory.getLog(UploadedFileAction.class);
    public ActionForward doAction(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        String fileName = "";
        Blob fileContent = null;
        String action = this.getAction(request);
        //log.info("action="+action);
        String id = request.getParameter("id");
        try
        {
            Transaction tx = null;        
            UploadedFileDAO uploadedFileDAO = new UploadedFileDAO();
            tx = HibernateSessionFactory.getSession().beginTransaction();
            //log.info("id="+id);
            UploadedFile uf = uploadedFileDAO.findById(id);
            //log.info("delete".equals(action));
            if("delete".equals(action))
            {
                //log.info("uf="+uf.getName());
                //uploadedFileDAO.delete(uf);
            }
            else
            {
                fileName = uf.getName();
                fileContent = uf.getContent();
            }
            tx.commit();
        }
        catch(Exception e)
        {
            throw e;
        }
        if("delete".equals(action))
        {
            ActionForward af = null;        
            String path = mapping.findForward(action).getPath();
            path = path.replace("!?",id.substring(1));
            af = new ActionForward(path);
            af.setRedirect(true);
            request.setAttribute("isFromInternal","Y");
            return af;
        }
        else
        {
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition","attachment;"+"filename="+new String(fileName.getBytes(),"GBK"));
            response.getOutputStream().write(fileContent.getBytes(1,(int)fileContent.length()));
            response.getOutputStream().flush();
        }
        return null;
    }    
    private void downloadFile(String fileId)
    {
        
    }
}
